﻿namespace RolePlayingGame
{
    using System;
    using System.IO;
    using System.Text;
    using System.Threading;

    public static class Intro
    {
        private const int BREAK_SPEED = 50;
        private const int LETTER_PAUSE_SPEED = 5;


        private static int width;
        private static int height;
        private static int nextLineCounter = 0;

        public static int playerChoose;

        public static int PlayIntro()
        {
            Sounds.Intro();
            Console.WindowHeight = 40;
            Console.WindowWidth = 150;
            Console.SetBufferSize(150, 70);
            using (StreamReader reader = new StreamReader("../../Intro/story.txt"))
            {
                Console.CursorVisible = false;
                int lines = int.Parse(reader.ReadLine());
                for (int i = 0; i < lines; i++)
                {
                    StringBuilder line = new StringBuilder();
                    line.Append(reader.ReadLine());
                    ManageLine(line);
                }
            }
            Logo.DisplayLogo();
            Console.Clear();
            NameEnter.EnterName();
            Console.Clear();
            return playerChoose;
        }

        private static void ManageLine(StringBuilder line)
        {
            if (line.ToString() == "<clear>")
            {
                nextLineCounter = 0;
                ConsoleClear();
            }
            else if (line.ToString() == "<break>")
            {
                Thread.Sleep(BREAK_SPEED);
            }
            else if (line.ToString() == "<red>")
            {
                Console.ForegroundColor = ConsoleColor.Red;
            }
            else if (line.ToString() == "<blue>")
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
            }
            else if (line.ToString() == "<green>")
            {
                Console.ForegroundColor = ConsoleColor.Green;
            }
            else if (line.ToString() == "<yellow>")
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
            }
            else if (line.ToString() == "</>")
            {
                Console.ResetColor();
            }
            else
            {
                nextLineCounter++;
                DisplayLine(line);
            }
        }

        private static void ConsoleClear()
        {
            Console.Clear();
        }

        private static void DisplayLine(StringBuilder line)
        {
            CalculateMiddle(line.Length);
            for (int i = 0; i < line.Length; i++)
            {
                Console.Write(line[i]);
                Thread.Sleep(LETTER_PAUSE_SPEED);
            }
            Console.WriteLine();
        }

        private static void CalculateMiddle(int stringLength)
        {
            int middleWidth = Console.WindowWidth / 2;
            width = middleWidth - (stringLength / 2);
            height = (Console.WindowHeight / 2) + nextLineCounter;
            Console.SetCursorPosition(width, height);
        }
    }
}
